﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.Actions;

namespace RibbonEditor.Controls.Actions
{
    public partial class UrlControl : UserControl, IControlRulable
    {
        private Url pageUrl;

        public Url PageUrl
        {
            get { return this.pageUrl; }
            set { this.pageUrl = value; }
        }

        public UrlControl()
        {
            try
            {
                InitializeComponent();

                this.ecWinMode.Values = Enum.GetValues(typeof(Enumerations.WinMode));
                this.pageUrl = new Url();
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public UrlControl(Url url)
            : this()
        {
            this.pageUrl = url;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.strAddress.Value = this.pageUrl.Address;
                this.strWinParams.Value = this.pageUrl.WinParams;
                this.bcPassParams.Value = this.pageUrl.PassParams;
                this.ecWinMode.SelectedValue = this.pageUrl.WinMode.ToString();
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.pageUrl.Address = this.strAddress.Value;
                    this.pageUrl.PassParams = this.bcPassParams.Value;
                    this.pageUrl.WinMode = (Enumerations.WinMode)Enum.Parse(typeof(Enumerations.WinMode), this.ecWinMode.SelectedValue);
                    this.pageUrl.WinParams = this.strWinParams.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void UrlControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.pageUrl.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
